package com.cf.curd.service;

/**
 * cf-game-BaseService
 *
 * @author <a href="2428922347@qq.com">Zhu mingming</a>
 * @info
 * @since 2018/3/1 18:14
 */


import com.baomidou.dynamic.datasource.DS;
import com.cf.crud.entity.BaseEntity;
import com.cf.crud.mapper.BaseMapper;
import com.cf.crud.service.AbsBaseService;
import com.cf.crud.service.BaseService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

/**
 * wx-hongbao-BaseService
 *
 * @author <a href="2428922347@qq.com">Zhu mingming</a>
 * @info
 * @since 2018/1/15 15:02
 */
public abstract class AbsDynamicBaseService<T extends BaseEntity, M extends BaseMapper<T>> extends AbsBaseService<T, M> implements BaseService<T> {


    @DS
    @Override
    public List<T> selectByIds(List<Long> ids) {
        Example example = new Example(getTargetClass());
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", ids);
        return selectByExample(example);
    }

    @DS
    @Override
    public T selectOne(long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @DS
    @Override
    public List<T> select(T bean) {
        return mapper.select(bean);
    }

    @DS
    @Override
    public Page<T> selectByPage(Integer page, Integer size) {
        PageHelper.startPage(page, size);
        return (Page) mapper.selectAll();
    }

    @DS
    @Override
    public Page<T> selectByPage(T bean, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        return (Page) mapper.select(bean);
    }

    @DS
    @Override
    public List<T> selectAll() {
        return this.mapper.selectAll();
    }

    @DS
    @Override
    public Page<T> selectByExample(Object example, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        return (Page<T>) this.mapper.selectByExample(example);
    }

    @DS
    @Override
    public Page<T> commonSearch(Map parameters, Integer page, Integer size) {
        Example searchCriteria = createSearchCriteria(parameters);
        return selectByExample(searchCriteria, page, size);
    }

    @DS
    @Override
    public List<T> selectByExample(Object example) {
        return selectByExample(example, 0, Integer.MAX_VALUE);
    }
}
